/*
#------------------------------------------------------------------------------
#-- Program Name:	[dbo].[fnIO_Extension]
#-- Purpose:		Determines the extension of a file; if the file doesn't 
#--                 exist it returns a best guess
#--	Last Update:	08/02/2012
#--					For a complete history - please review comments in Version
#--					Control.
#------------------------------------------------------------------------------
*/

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString fnIO_Extension(string filePath)
    {
        if (fnIO_FileExists(filePath))
        {
            string Ext = new System.IO.FileInfo(filePath).Extension.ToString();
            if (Ext.Substring(0, 1).Equals("."))
                return Ext;
            else
                return ("." + Ext);
        }
        else
        {
            string[] parts = filePath.Replace(fnIO_DirectoryName(filePath).ToString(), "").Split(".".ToCharArray());
            if (parts.Length.Equals(1))
                return "";
            else
                return "." + parts[parts.Length - 1];
        }
    }
}
